using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Admin.NET.Application.Service.AliPay;

/// <summary>
/// 支付宝特邀商户服务商
/// </summary>
[ApiDescriptionSettings(new string[] { "业务应用" }, Order = 100)]
public class AliPaySubService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<AliPaySub> _rep;

	public AliPaySubService(SqlSugarRepository<AliPaySub> rep)
	{
		_rep = rep;
	}

	/// <summary>
	/// 分页查询子商户号
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/aliPaySub/page")]
	public async Task<dynamic> Page([FromQuery] AliSubPageInput input)
	{
		ISugarQueryable<AliPaySub> query = _rep.Context.Queryable<AliPaySub>().WhereIF(!string.IsNullOrWhiteSpace(input.SubName), (AliPaySub u) => u.SubName.Contains(input.SubName.Trim()));
		query = query.OrderBuilder(input);
		SqlSugarPagedList<AliPaySub> list = await query.ToPagedListAsync(input.Page, input.PageSize);
		List<AliPaySub> listItem = list.Items.ToList();
		string pattern = "(?<=[\\S]{4})\\S(?=[\\S]{4})";
		Regex rgx = new Regex(pattern);
		listItem.ForEach(delegate(AliPaySub u)
		{
			if (u.AppAuthToken != null && u.AppAuthToken.Length > 8)
			{
				u.AppAuthToken = rgx.Replace(u.AppAuthToken, "*");
			}
			if (u.SubAppId != null && u.SubAppId.Length > 8)
			{
				u.SubAppId = rgx.Replace(u.SubAppId, "*");
			}
		});
		return list;
	}

	/// <summary>
	/// 增加子商户
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/aliPaySub/add")]
	public async Task Add(AddAliSubInput input)
	{
		AliPaySub entity = input.Adapt<AliPaySub>();
		await _rep.InsertAsync(entity);
	}

	/// <summary>
	/// 删除子商户
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/aliPaySub/delete")]
	public async Task Delete(DeleteAliSubInput input)
	{
		AliPaySub entity = await _rep.GetFirstAsync((AliPaySub u) => u.Id == input.Id);
		if (entity != null)
		{
			await _rep.DeleteAsync(entity);
		}
	}

	/// <summary>
	/// 更新子商户
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/aliPaySub/edit")]
	public async Task Update(UpdateAliSubInput input)
	{
		AliPaySub entity = input.Adapt<AliPaySub>();
		AliPaySub model = await _rep.GetFirstAsync((AliPaySub u) => u.Id == input.Id);
		if (model != null)
		{
			if (entity.AppAuthToken != null && !entity.AppAuthToken.Contains("*"))
			{
				model.AppAuthToken = entity.AppAuthToken;
			}
			if (entity.SubAppId != null && !entity.SubAppId.Contains("*"))
			{
				model.SubAppId = entity.SubAppId;
			}
			model.Remark = entity.Remark;
			model.SubName = entity.SubName;
			model.SubTenantId = entity.SubTenantId;
			model.PID = entity.PID;
			await _rep.UpdateAsync(model);
			return;
		}
		throw Oops.Bah("当前数据不存在");
	}
}
